// 章

const chapter = require("../../model/catelogue/chapter")

const question = require("../../model/question/question")

// 引入外部模块 获取当前时间
const { getTime } = require("../../utils/gettime")

// const checkParams = (data) => {
//     if (!data) return { msg: "操作失败", code: "400" }
// }

let attributes = ["id", "title", "creator", "createdate", "updator", "updated", "catalogue_id"]

// 根据id 查询目录 里面的 试题
exports.getChapterById = async ({ id, pageNum, pageSize }) => {
    try {
        id = Number(id)
        let res = await chapter.findOne({
            offset: pageNum * pageSize - pageSize,
            limit: pageSize,
            attributes,
            include: [{
                // model: question,
                association: chapter.hasMany(question, { foreginKey: "chapter_id", targetKey: "id" }),
                where: { chapter_id: id }
            }],
        })
        if (res) {
            res.questions.map(item => {
                if(item.type_id == 1){
                    item.options = JSON.parse(item.options)
                    item.answer = JSON.parse(item.answer)
                }
            })
            return { msg: "获取成功", code: "200", data: res }
        } else {
            return { msg: "获取失败 无数据", code: "400" }
        }
    } catch (err) { return { msg: "操作失败", code: "400", err } }
}

exports.getChapterAll = async () => {
    let res = await chapter.findAll({attributes:["title","id"]})
    if (res) return { msg: "查询成功", code: "200", data: res }
    return { msg: "查询失败", code: "400" }
}

//  查询目录列表
exports.getChapterList = async ({ pageNum, pageSize }) => {
    let res = await chapter.findAndCountAll({
        offset: pageNum * pageSize - pageSize,
        limit: pageSize,
        attributes
    })
    if (res) return { msg: "查询成功", code: "200", data: res }
    return { msg: "查询失败", code: "400" }
}

// 添加目录
exports.createChapter = async ({ title, creator, catalogue_id }) => {
    try {
        let isCreateFindOne = await chapter.findOne({ where: { title } })
        if (isCreateFindOne) return { msg: "重复的目录名称", code: "400" }
        const newData = {}
        newData.title = title
        newData.catalogueId = catalogue_id
        if (creator) newData.creator = creator
        newData.createdate = getTime()

        // return newData
        // return catalogue_id
        let isCreate = await chapter.create(newData)
        if (isCreate) return { msg: "添加成功", code: "200" }
        return { msg: "操作失败", code: "400" }
    } catch (err) { return { msg: "操作失败", code: "400", err } }
}

// 编辑目录
exports.editChapter = async ({ id, title, updator, catalogue_id }) => {
    try {
        let newData = {};
        if (updator) newData.updator = updator
        if (catalogue_id) newData.CatalogueId = Number(catalogue_id)
        newData.updated = getTime()
        if (title) {
            let isTitle = await chapter.findOne({ where: { title }, attributes: ["title"] })
            if (isTitle) {
                let isId = await chapter.findOne({ where: { id }, attributes: ["title"] })
                if (isId.title !== isTitle.title) return { msg: "重复的章节名", code: "400" }
            } else {
                newData.title = title
            }
        }
        let isUpdate = await chapter.update(newData, { where: { id } })
        if (isUpdate) {
            let res = await chapter.findOne({ attributes, where: { id } })
            return { msg: "编辑成功", code: "200", data: res }
        } else {
            return { msg: "操作失败", code: "400" }
        }
    } catch (err) { return { msg: "操作失败", code: "400", err } }
}

// 删除目录
exports.removeChapter = async ({ id }) => {
    if (!id) return { msg: "删除失败 id is undefind", code: "400" }
    let isRemove = await chapter.destroy({ where: { id } })
    if (isRemove) return { msg: "删除成功", code: "200" }
    return { msg: "删除失败", code: "400" }
}